package com.superc.model.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.superc.R;

public class ListItemAdapter<T> extends BaseAdapter implements  OnItemSelectedListener {

	private List<T> datas;
	private LayoutInflater inflater=null;
	private int resourceItemID=0;
	private IAdapaterViewCallback<T> callback=null;
	private int selectedItemPosition=-1;
	private View oldSelectedView=null;
	
	public T getSelectedItem(){
		if(selectedItemPosition!=-1 && selectedItemPosition<datas.size()){
			return datas.get(selectedItemPosition);
		}
		return null;
	}

	public int getSelectedItemPosition(){
		return selectedItemPosition;
	}
	
	public void setSelectedItemPosition(int position){
		selectedItemPosition=position;
	}
	
	@Override
	public void onItemSelected(AdapterView<?> parent, View arg1,
            int position, long arg3) {
		selectedItemPosition=position;
		if(oldSelectedView!=null){
			oldSelectedView.setBackgroundColor(Color.GRAY);
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}

	public void addSelectedFeature(ListView owner){
		owner.setOnItemSelectedListener(this);
	}
	
	public int getResourceItemID() {
		return resourceItemID;
	}

	public void setResourceItemID(int resourceItemID) {
		this.resourceItemID = resourceItemID;
	}

	public List<T> getDatas(){
		return datas;
	}
	
	public ListItemAdapter(Context context,int resourceID, List<T> datas,IAdapaterViewCallback<T> viewCallBack){
		this.datas=datas;
		this.resourceItemID=resourceID;
		inflater=LayoutInflater.from(context);
		this.callback=viewCallBack;
	}
	
	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		boolean isFirstCreate=false;
		if(convertView==null){
			convertView =inflater.inflate(resourceItemID,null);
			isFirstCreate=true;
		}
		if(callback!=null){
			callback.OnCreateView(convertView, datas.get(position), position, isFirstCreate);
		}
		convertView.setTag(R.color.colorListItem,position);
		convertView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				v.setBackgroundResource(R.drawable.listviewcolor);
				selectedItemPosition=Integer.parseInt(v.getTag(R.color.colorListItem).toString());
				if(oldSelectedView!=null){
					oldSelectedView.setBackgroundColor(Color.TRANSPARENT);
				}
				oldSelectedView=v;
				if(callback!=null){
					callback.OnViewClickListener(v);
				}
			}
		});
		return convertView;
	}


}
